package com.learn.metrics.type.histogram;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Histogram;
import com.learn.metrics.util.MetricUtil;

/**
 * 直方图
 * <p>用来统计输入指标值中的分布, 最小值、最大值、平均值、百分之75等等</p>
 * @author XuYang
 * @since 2025/2/19 9:47
 */
public class HistogramExample {

    public static void start(Integer interval) {
        MetricUtil.startReporter(interval);
        Histogram histogram = new Histogram(new ExponentiallyDecayingReservoir());
        MetricUtil.REGISTRY.histogram("gauges", () -> histogram);
        int count = 0;
        while (count++ < 100) {
            histogram.update(RandomUtil.randomInt(100000));
            ThreadUtil.sleep(RandomUtil.randomInt(500, 1000));
        }

        // 输入指标总数
        histogram.getCount();
        // 输入指标最小值
        histogram.getSnapshot().getMin();
        // 输入指标最大值
        histogram.getSnapshot().getMax();
        // 输入指标平均值
        histogram.getSnapshot().getMean();
        // 输入指标值中的标准偏差值
        histogram.getSnapshot().getStdDev();
        // 输入指标中位数
        histogram.getSnapshot().getMedian();
        // 输入指标75百分位
        histogram.getSnapshot().get75thPercentile();
        // 输入指标95百分位
        histogram.getSnapshot().get95thPercentile();
        // 输入指标98百分位
        histogram.getSnapshot().get98thPercentile();
        // 输入指标99百分位
        histogram.getSnapshot().get99thPercentile();
        // 输入指标99.9百分位
        histogram.getSnapshot().get999thPercentile();

    }
}
